﻿@{
@*Get userid*@
    var uid = KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString();
    Guid? user_id;
    if (uid != "")
    {
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
    else
    {
        KimPhongCRM.Controllers.BaseController bcontrol = new KimPhongCRM.Controllers.BaseController();
        string un = User.Identity.Name;
        bcontrol.ressUserID(un);
        user_id = new Guid(KimPhongCRM.Lib.SessionData.GetSession("sdUserId").ToString());
    }
@*Get userid*@    
    var listUser = ViewData["listUser"] as List<KimPhongCRM.Models.sp_Select_Combobox_User_Result>;
    var grid = Html.DevExpress().GridView(settings =>
    {
        settings.Name = "ListPermission";
        settings.CallbackRouteValues = new { Controller = "Permission", Action = "ListPermissionPartial" };

        settings.SettingsEditing.AddNewRowRouteValues = new { Controller = "Permission", Action = "ListPermissionPartialAddNew" };
        settings.SettingsEditing.UpdateRowRouteValues = new { Controller = "Permission", Action = "ListPermissionPartialUpdate" };
        settings.SettingsEditing.DeleteRowRouteValues = new { Controller = "Permission", Action = "ListPermissionPartialDelete" };
        settings.CommandColumn.ButtonType = System.Web.UI.WebControls.ButtonType.Image;
        settings.CommandColumn.SetColVisibleIndex(0);
        settings.CommandColumn.Width = 100;
        settings.CommandColumn.SetHeaderCaptionTemplateContent("Tùy chọn");
        settings.CommandColumn.FixedStyle = GridViewColumnFixedStyle.Left;

        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.NewButton.Image.Url = @Url.Content("~/Content/img/icon-24-new.png");
        settings.CommandColumn.NewButton.Image.ToolTip = "New";
        settings.CommandColumn.NewButton.Image.AlternateText = "Thêm";

        settings.CommandColumn.EditButton.Visible = true;
        settings.CommandColumn.EditButton.Image.Url = @Url.Content("~/Content/img/icon-24-edit.png");
        settings.CommandColumn.EditButton.Image.ToolTip = "Eidt";
        settings.CommandColumn.EditButton.Image.AlternateText = "Sửa";

        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.DeleteButton.Image.Url = @Url.Content("~/Content/img/icon-24-delete.png");
        settings.CommandColumn.DeleteButton.Image.ToolTip = "Delete";
        settings.CommandColumn.DeleteButton.Image.AlternateText = "Xóa";

        settings.CommandColumn.UpdateButton.Visible = true;
        settings.CommandColumn.UpdateButton.Image.Url = @Url.Content("~/Content/img/icon-24-ok.png");
        settings.CommandColumn.UpdateButton.Image.ToolTip = "Update";
        settings.CommandColumn.UpdateButton.Image.AlternateText = "Lưu";

        settings.CommandColumn.CancelButton.Visible = true;
        settings.CommandColumn.CancelButton.Image.Url = @Url.Content("~/Content/img/icon-24-cancel.png");
        settings.CommandColumn.CancelButton.Image.ToolTip = "Cancel";
        settings.CommandColumn.CancelButton.Image.AlternateText = "Hủy";


        settings.SettingsEditing.Mode = GridViewEditingMode.PopupEditForm;
        settings.SettingsPopup.EditForm.AllowResize = true;
        settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
        settings.SettingsPopup.EditForm.Width = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Height = System.Web.UI.WebControls.Unit.Percentage(90);
        settings.SettingsPopup.EditForm.Modal = true;
        settings.SettingsBehavior.ConfirmDelete = true;

        settings.CommandColumn.Visible = true;
        settings.CommandColumn.NewButton.Visible = true;
        settings.CommandColumn.DeleteButton.Visible = true;
        settings.CommandColumn.EditButton.Visible = true;

        settings.SettingsPager.Visible = true;
        settings.Settings.ShowGroupPanel = true;
        settings.Settings.ShowFilterRow = true;
        settings.SettingsBehavior.AllowSelectByRowClick = true;
        settings.Settings.HorizontalScrollBarMode = ScrollBarMode.Auto;
        settings.Settings.VerticalScrollBarMode = ScrollBarMode.Hidden;
        settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

        settings.KeyFieldName = "permId";

        settings.SettingsPager.Visible = true;
        settings.Settings.ShowGroupPanel = true;
        settings.Settings.ShowFilterRow = true;
        settings.SettingsBehavior.AllowSelectByRowClick = true;

        settings.Columns.Add(column =>
        {
            column.FieldName = "permName";
            column.Caption = "Tên truy cập";
            column.ColumnType = MVCxGridViewColumnType.TextBox;
            column.FixedStyle = GridViewColumnFixedStyle.Left;
            column.Visible = true;
            column.Width = System.Web.UI.WebControls.Unit.Percentage(100);
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "controller";
            column.Caption = "Controller";
            column.ColumnType = MVCxGridViewColumnType.TextBox;
            column.FixedStyle = GridViewColumnFixedStyle.Left;
            column.Visible = true;
            column.Width = 100;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "action";
            column.Caption = "Action";
            column.ColumnType = MVCxGridViewColumnType.TextBox;
            column.Visible = true;
            column.Width = 100;
        });

        settings.Columns.Add(column =>
        {
            column.FieldName = "ngaySuaCuoi";
            column.Caption = "Ngày sửa cuối";
            column.ColumnType = MVCxGridViewColumnType.DateEdit;
            var dateProperties = column.PropertiesEdit as DateEditProperties;
            dateProperties.UseMaskBehavior = true;
            dateProperties.EditFormat = EditFormat.Custom;
            dateProperties.EditFormatString = "dd/MM/yyyy hh:mm tt";
            column.ReadOnly = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "nguoiSuaCuoi";
            column.Caption = "Người sửa cuối";
            column.ColumnType = MVCxGridViewColumnType.ComboBox;
            var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            foreach (var value in listUser)
            {
                comboBoxProperties.Items.Add(value.UserName, value.UserId);
            }
            comboBoxProperties.ValueType = typeof(Guid);
            column.ReadOnly = true;
            column.Width = 100;
        });
        settings.Columns.Add(column =>
        {
            column.FieldName = "isChildAction";
            column.Caption = "ChildAction";
            column.ColumnType = MVCxGridViewColumnType.CheckBox;
            column.Width = 60;
            column.SetDataItemTemplateContent(c =>
            {
                bool actived = (bool)DataBinder.Eval(c.DataItem, "isChildAction");
                ViewContext.Writer.Write(
                    "<i style=\"color:blue\" class=\"font-icon-" + (actived ? "ok " : "") + "\"></i>"
                );
            });
        });
        
        settings.Columns.Add(column =>
        {
            column.FieldName = "isActive";
            column.Caption = "Active";
            column.ColumnType = MVCxGridViewColumnType.CheckBox;
            column.Width = 60;
            column.SetDataItemTemplateContent(c =>
            {
                bool actived = (bool)DataBinder.Eval(c.DataItem, "isActive");
                object perm_obj = DataBinder.Eval(c.DataItem, "permId");
                string updateLink = Url.Action("ToogleActive", "Base", new { keyvalue = perm_obj, active = !actived, tablename = "tblPermission", keyname = "permId" });
                ViewContext.Writer.Write(
                    "<a href=\"" + updateLink + "\" id=\"id_button_active_" + perm_obj + "\" class=\"button btn-active\"><i class=\"font-icon-" + (actived ? "check" : "check-empty") + "\"></i></a>"
                );
            });
        });
    });
    if (ViewData["EditError"] != null)
    {
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
}
@grid.Bind(Model).GetHtml()